﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using VideoClassLibrary.Model;

namespace VideoClassLibrary.BusinessLogicLayer
{
   public class SVR_IPC_Module
    {
       	/// <summary>
		/// 增加一个SVR
		/// </summary>
       public int AddSVRInfo(SVRInfo_Table model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into SVRInfo_Table(");
			strSql.Append("SVRName,SVRIP,SVRPort,SVRUser,SVRPwd,SVRStatus)");

			strSql.Append(" values (");
			strSql.Append("@SVRName,@SVRIP,@SVRPort,@SVRUser,@SVRPwd,@SVRStatus)");
			strSql.Append(";select @@IDENTITY");
            Database db = VDataBase.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "SVRName", DbType.AnsiString, model.SVRName);
			db.AddInParameter(dbCommand, "SVRIP", DbType.AnsiString, model.SVRIP);
			db.AddInParameter(dbCommand, "SVRPort", DbType.Int32, model.SVRPort);
			db.AddInParameter(dbCommand, "SVRUser", DbType.AnsiString, model.SVRUser);
			db.AddInParameter(dbCommand, "SVRPwd", DbType.AnsiString, model.SVRPwd);
			db.AddInParameter(dbCommand, "SVRStatus", DbType.Int32, model.SVRStatus);
			int result;
			object obj = db.ExecuteScalar(dbCommand);
			if(!int.TryParse(obj.ToString(),out result))
			{
				return 0;
			}
			return result;
		}
		/// <summary>
		/// 更新SVR的配置数据
		/// </summary>
        public void UpdateSVRInfo(SVRInfo_Table model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SVRInfo_Table set ");
			strSql.Append("SVRName=@SVRName,");
			strSql.Append("SVRIP=@SVRIP,");
			strSql.Append("SVRPort=@SVRPort,");
			strSql.Append("SVRUser=@SVRUser,");
			strSql.Append("SVRPwd=@SVRPwd,");
			strSql.Append("SVRStatus=@SVRStatus");
			strSql.Append(" where SVRID=@SVRID ");
            Database db = VDataBase.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "SVRID", DbType.Int32, model.SVRID);
			db.AddInParameter(dbCommand, "SVRName", DbType.AnsiString, model.SVRName);
			db.AddInParameter(dbCommand, "SVRIP", DbType.AnsiString, model.SVRIP);
			db.AddInParameter(dbCommand, "SVRPort", DbType.Int32, model.SVRPort);
			db.AddInParameter(dbCommand, "SVRUser", DbType.AnsiString, model.SVRUser);
			db.AddInParameter(dbCommand, "SVRPwd", DbType.AnsiString, model.SVRPwd);
			db.AddInParameter(dbCommand, "SVRStatus", DbType.Int32, model.SVRStatus);
			db.ExecuteNonQuery(dbCommand);

		}

		/// <summary>
		/// 得到一个SVR对象实体
		/// </summary>
        public SVRInfo_Table GetSVRInfoModel(int SVRID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select SVRID,SVRName,SVRIP,SVRPort,SVRUser,SVRPwd,SVRStatus from SVRInfo_Table ");
			strSql.Append(" where SVRID=@SVRID ");
            Database db = VDataBase.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "SVRID", DbType.Int32,SVRID);
		    SVRInfo_Table model=null;
			using (IDataReader dataReader = db.ExecuteReader(dbCommand))
			{
				if(dataReader.Read())
				{
					model=ReaderBind(dataReader);
				}
			}
			return model;
		}

		/// <summary>
		/// 获得SVR信息列表
		/// </summary>
        public DataSet GetSVRInfoList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select SVRID,SVRName,SVRIP,SVRPort,SVRUser,SVRPwd,SVRStatus, ");
            strSql.Append(" (case SVRStatus when 0 then '停止' when 1 then '运行'  when 2 then '挂起' else '' end ) SVRStatusName");
            strSql.Append("  FROM SVRInfo_Table where 1=1 ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(strWhere);
			}
            Database db = VDataBase.CreateDatabase();
			return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
		}

        /// <summary>
        /// SVR对象实体绑定数据
        /// </summary>
        public SVRInfo_Table ReaderBind(IDataReader dataReader)
        {
            SVRInfo_Table model = new SVRInfo_Table();
            object ojb;
            ojb = dataReader["SVRID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SVRID = (int)ojb;
            }
            model.SVRName = dataReader["SVRName"].ToString();
            model.SVRIP = dataReader["SVRIP"].ToString();
            ojb = dataReader["SVRPort"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SVRPort = (int)ojb;
            }
            model.SVRUser = dataReader["SVRUser"].ToString();
            model.SVRPwd = dataReader["SVRPwd"].ToString();
            ojb = dataReader["SVRStatus"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.SVRStatus = (int)ojb;
            }
            return model;
        }
    }
}
